<?php
class Catalog extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
            return parent::model($className);
	}

	public function tableName()
	{
            return 'Catalog';
	}

	public function rules()
	{

            return array(
			array('title, catalog_category_id, url, m_k, m_d, description, l_id', 'required'),
			array('catalog_category_id, visible, hit, l_id, discount, rating', 'numerical', 'integerOnly'=>true),
			array('price', 'numerical'),
			array('title, url', 'length', 'max'=>255),
			array('properties', 'safe'),
			array('id, title, catalog_category_id, visible, hit, url, m_k, m_d, description, properties, l_id, price, discount, rating', 'safe', 'on'=>'search'),
		);
	}
        
	public function relations()
	{
            return array(
			'catalogCategory' => array(self::BELONGS_TO, 'CatalogCategory', 'catalog_category_id'),
			'l' => array(self::BELONGS_TO, 'Language', 'l_id'),
			'catalogCustomFieldValues' => array(self::HAS_MANY, 'CatalogCustomFieldValue', 'c_id'),
			'catalogImages' => array(self::HAS_MANY, 'CatalogImage', 'catalog_id'),
		);
	}
        

	public function attributeLabels()
	{
            return array(
                    'id' => Yii::t('system','ID'),
                    'title' => Yii::t('system','Title'),
                    'catalog_category_id' => Yii::t('system','Catalog Category'),
                    'visible' => Yii::t('system','Visible'),
                    'hit' => Yii::t('system','Hit'),
                    'url' => Yii::t('system','Url'),
                    'm_k' => Yii::t('system','M K'),
                    'm_d' => Yii::t('system','M D'),
                    'description' => Yii::t('system','Description'),
                    'properties' => Yii::t('system','Properties'),
                    'l_id' => Yii::t('system','L'),
                    'price' => Yii::t('system','Price'),
                    'discount' => Yii::t('system','Discount'),
                    'rating' => Yii::t('system','Rating'),
            );
	}
        
        
        //!!
         public static function getCatalogByCategory($category){
            //Выборка
            $command=Yii::app()->db->createCommand()
            ->select('*')
            ->from('Catalog c')
            ->where('c.l_id = :language AND c.catalog_category_id = :category_id',
                    array(
                        ':language' => Language::getId(),
                        ':category_id'=>$category,
                    ))
            ->queryAll();
            return $command;
        }
        
         public static function getAllCatalog(){
            //Выборка
            $command=Yii::app()->db->createCommand()
            ->select('*')
            ->from('Catalog c')
            ->where('c.l_id = :language',
                    array(
                        ':language' => Language::getId(),
                    ))
            ->queryAll();
            return $command;
        }

        
        public function dropDownItemsAll(){
            $items= Array();
            $model = self::model()->findAll();
            foreach ($model as $value) {
                $items[$value->id] = $value->id;
            }
            return $items;
        }
        
        
                public function search()
	{	
            $criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('catalog_category_id',$this->catalog_category_id);
		$criteria->compare('visible',$this->visible);
		$criteria->compare('hit',$this->hit);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('m_k',$this->m_k,true);
		$criteria->compare('m_d',$this->m_d,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('properties',$this->properties,true);
		$criteria->compare('l_id',$this->l_id);
		$criteria->compare('price',$this->price);
		$criteria->compare('discount',$this->discount);
		$criteria->compare('rating',$this->rating);

            return new CActiveDataProvider($this, array(
                    'criteria'=>$criteria,
            ));
	}
}


//    public static function allLang(){
//        $dataReader=Yii::app()->db->createCommand()
//        ->select('l,name')
//        ->from(array('Language'))
//        ->queryAll();
//        return $dataReader;
//    }
//    
//    public static function countLang(){
//        $sql = "SELECT COUNT(*) as count FROM `Language`";
//        $dataReader=Yii::app()->db->createCommand($sql)->queryRow();
//        return $dataReader['count'];
//    }
//    
//    public static function allExcept($except_id){
//        $dataReader=Yii::app()->db->createCommand()
//        ->select('*')
//        ->from(array('Language'))
//        ->where(array('and', '`primary` <> :primary', array('not in','id',$except_id)),array(':primary'=>'1'))
//        ->queryAll();
//        return $dataReader;
//    }